import sys
import pygame
from random import randint

#引入pygame中所有常量，比如 QUIT
from pygame.locals import *

# 比例缩放
SHUFANG_N=2
SHUFANG=1/SHUFANG_N

# 红色边框背景
# BG=(200,50,60)

# 白色边框背景
# BG=(255,255,255)

# 黑色边框背景
# BG=(0,0,0)

# 自定义边框背景
BG=(153,217,234)

# 我的电脑全屏
BACKW=1768
BACKH=1080

# 比例缩放屏幕
# BACKW=1768//SHUFANG_N
# BACKH=1080//SHUFANG_N

def liuying(wz,surface_love,bg=BG):
    pygame.display.flip()
    screen.fill(bg)
    screen.blit(screen_2,(BACKW//20,BACKH//20))
    screen_2.fill(bg_2)
    screen.blit(surface_love, wz)
    pygame.display.update()

class Snake_ly(pygame.sprite.Sprite):
    #定义构造函数
    def __init__(self,filename,location):
        # 调父类来初始化子类
        pygame.sprite.Sprite.__init__(self)
        # 加载图片
        self.image = pygame.image.load(filename)
        # 缩放
        self.size=0.2
        self.image=pygame.transform.rotozoom(self.image,0,self.size*SHUFANG)
        # 获取图片rect区域
        self.rect = self.image.get_rect()
        # 设置位置
        self.rect.topleft=location

class Snake_xyd(pygame.sprite.Sprite):
    #定义构造函数
    def __init__(self,filename,location):
        # 调父类来初始化子类
        pygame.sprite.Sprite.__init__(self)
        # 加载图片
        self.image = pygame.image.load(filename)
        # 缩放
        self.size =0.23
        self.image=pygame.transform.rotozoom(self.image,0,self.size*SHUFANG)
        # 获取图片rect区域
        self.rect = self.image.get_rect()
        # 设置位置
        self.rect.topleft=location

class Snake_sw(pygame.sprite.Sprite):
    #定义构造函数
    def __init__(self,filename,location):
        # 调父类来初始化子类
        pygame.sprite.Sprite.__init__(self)
        # 加载图片
        self.image = pygame.image.load(filename)
        # # 缩放
        self.size =0.5
        self.image=pygame.transform.rotozoom(self.image,0,self.size*SHUFANG)
        # 获取图片rect区域
        self.rect = self.image.get_rect()
        # 设置位置
        self.rect.topleft=location

if __name__== "__main__":
    # 初始化
    pygame.init()

    # 信用点生成基准
    XYD_BACEX=BACKW-BACKW//20
    XYD_BACEY=BACKH-BACKH//20
    XYD_SETX=(BACKW//20,XYD_BACEX-153//SHUFANG_N)
    XYD_SETY=(BACKH//20,XYD_BACEY-139//SHUFANG_N)

    # 流萤生成基准
    LY_SETX=BACKW//20
    LY_SETY=BACKH//20

    #信用点计数 
    num_xydnum=0
    sw_nandu=1


    #计数 
    sw_move_rand=0

    # 提示文本
    screen = pygame.display.set_mode((BACKW,BACKH))
    #填充主窗口的背景颜色
    screen.fill((153,217,234))
    #设置窗口标题
    pygame.display.set_caption('简介')
    # 字体文件路径 C:/Windows/Fonts/simhei.ttf
    f = pygame.font.Font('.\STHUPO.TTF',50)
    # render(text, antialias, color, background=None) -> Surface
    tishi_txt_1="按 |回车| 开始游戏，本游戏作者ALX,"
    tishi_txt_2="小键盘 |上下左右| 可以操作人物“流萤”移动"
    tishi_txt_3="数字|1|:加速    数字|2|:减速    |ESC|:退出游戏"
    tishi_txt_4=f"信用点{num_xydnum}"
    tishi_txt_5=f"对...不起..."
    tishi_txt_6="1:再来一局                  2:退出"
    text_1 = f.render(tishi_txt_1,True,(0,0,0),(153,217,234))
    text_2 = f.render(tishi_txt_2,True,(125,125,125),(153,217,234))
    text_3 = f.render(tishi_txt_3,True,(50,20,200),(153,217,234))
    text_4 = f.render(tishi_txt_4,True,(50,20,200),(153,217,234))
    text_5 = f.render(tishi_txt_5,True,(200,50,50))
    text_6 = f.render(tishi_txt_6,True,(200,50,50))
    #获得显示对象的 rect区域大小
    textRect_1 =text_1.get_rect() 
    textRect_2 =text_2.get_rect() 
    textRect_3 =text_3.get_rect() 
    textRect_4 =text_4.get_rect() 
    textRect_5 =text_5.get_rect() 
    textRect_6 =text_6.get_rect() 
    #设置显示对象
    textRect_1.center = (BACKW//2,BACKH//2-200)
    textRect_2.center = (BACKW//2,BACKH//2-100)
    textRect_3.center = (BACKW//2,BACKH//2)
    textRect_4.center = (150,50)
    textRect_5.center = (BACKW//2,BACKH//2)
    textRect_6.center = (BACKW//2,BACKH//2+300)

    screen.blit(text_1,textRect_1)
    screen.blit(text_2,textRect_2)
    screen.blit(text_3,textRect_3)
    

    # # 得分
    # # screen.blit(text_4,textRect_4)

    # while True:
    #     break_txt=1
    #     # 循环获取事件，监听事件
    #     for event in pygame.event.get():
    #         # 判断用户是否点了关闭按钮
    #         # x退出
    #         if event.type == QUIT:
    #             exit()
    #         #esc退出 
    #         if event.type ==KEYDOWN:
    #             if event.key == K_ESCAPE:
    #                 pygame.quit()
    #                 sys.exit()
    #             # 回车继续
    #             if event.key == K_RETURN:
    #                 break_txt=0
    #     if break_txt==0:
    #         break
    #     pygame.display.flip() #更新屏幕内容



    # 窗口名字
    pygame.display.set_caption('流萤')


    # 底层
    size = width, height = BACKW,BACKH
    bg = BG
    bg_2 = (255,255,255)


# 全屏模式

    # screen=pygame.display.set_mode(size,pygame.FULLSCREEN)
    # SW_LY=(80,0)

# 正常比例
    screen=pygame.display.set_mode(size)
    SW_LY=(0,0)


    screen_2=pygame.transform.rotozoom(screen,0,0.9)


    # 创建图像
    # 死亡
    surface_lyj =pygame.image.load(".\shell.jpg")
    # surface_lyj=pygame.transform.rotozoom(surface_lyj,0,0.1)

    die_ly=0

# 第一版
    # # 流萤
    # surface_lovey =pygame.image.load(".\lovey.png")
    # surface_lovey =pygame.transform.rotozoom(surface_lovey,0,0.2)
    # surface_lovey=pygame.transform.rotozoom(surface_lovey,0,SHUFANG)

    # surface_lovez =pygame.image.load(".\lovez.png")
    # surface_lovez =pygame.transform.rotozoom(surface_lovez,0,0.2)
    # surface_lovez=pygame.transform.rotozoom(surface_lovez,0,SHUFANG)

    # # 信用点
    # surface_xyd =pygame.image.load("xyd_2.png")
    # surface_xyd =pygame.transform.rotozoom(surface_xyd,0,0.23)
    # surface_xyd=pygame.transform.rotozoom(surface_xyd,0,SHUFANG)



    # # 生成初始流萤
    # surface_love=surface_lovey
    # screen.blit(surface_love,(LY_SETX,LY_SETY))
    # # screen.blit(surface_xyd,(800,500))



# 第二版
    surface_lovey_tp =".\lovey.png"
    surface_lovez_tp =".\lovez.png"
    surface_xyd_tp="xyd_2.png"
    surface_love_tp=".\lovey.png"
    surface_sw_tp=".\sw.png"

    surface_lovey=Snake_ly(surface_lovey_tp,(LY_SETX,LY_SETY))
    surface_lovez=Snake_ly(surface_lovez_tp,(LY_SETX,LY_SETY))
    surface_love=Snake_ly(surface_love_tp,(LY_SETX,LY_SETY))
    surface_sw=Snake_sw(surface_sw_tp,(BACKW-400,BACKH-300))
    
    screen.blit(surface_love.image,surface_love.rect)
    

    # 获取图像的位置
    position = surface_love.rect
    position[0]=LY_SETX
    position[1]=LY_SETY


    # 控制上下左右的
    a_sxzy=0
    num_move=1

    # 古希腊掌管信用点的列表
    xyd_list=[]

    # 速度
    sudu=1
    #创建时钟对象（控制游戏的FPS）
    clock = pygame.time.Clock()


    sizedat=0.2
    # 主循环
    while True:
        
        #通过时钟对象，指定循环频率，每秒循环180次
        clock.tick(180)
        # 控制移动的变量
        site_move = [0, 0]
           


        for event in pygame.event.get():
            # x退出
            if event.type == QUIT:
                exit()
            #esc退出 
            if event.type ==KEYDOWN:
                if event.key == K_ESCAPE:
                    pygame.quit()
                    sys.exit()
            if event.type ==KEYDOWN:
                if event.key == K_UP:
                    # print('上')
                    a_sxzy=1
                    num_move=-sudu
                if event.key == K_DOWN:
                    # print('下')
                    a_sxzy=1
                    num_move=sudu
                if event.key == K_RIGHT:
                    # print('右')
                    surface_love.image=surface_lovey.image
                    a_sxzy=0
                    num_move=sudu
                if event.key == K_LEFT:
                    # print('左')
                    surface_love.image=surface_lovez.image
                    num_move=-sudu
                    a_sxzy=0
                 # 按空格随机生成信用点
                if event.key == K_SPACE:
                    xyd_x=randint(*XYD_SETX)
                    xyd_y=randint(*XYD_SETY)
                    xyd_list.append((xyd_x,xyd_y))

                # 1加速
                if event.key == K_1:
                    sudu+=1
                    if num_move>=0:
                        num_move=sudu
                    else :num_move=-sudu

                # 2减速
                if event.key == K_2:
                    if sudu>0:
                        sudu-=1
                    if num_move>=0:
                        num_move=sudu
                    else :num_move=-sudu
            # 鼠标移动流萤 
            if event.type == MOUSEBUTTONDOWN:
                    # pos 获取鼠标当前位置
                    # print('鼠标按下',event.pos)
                    position[0]=event.pos[0]
                    position[1]=event.pos[1]
                    liuying(event.pos,surface_love.image)
                    while True:
                        sd=0
                        if event.type == MOUSEMOTION:
                            liuying(event.pos,surface_love.image)
                            position[0]=event.pos[0]
                            position[1]=event.pos[1]
                        for event in pygame.event.get():
                            if event.type == MOUSEBUTTONUP:
                                sd=1
                        if sd==1:
                            break


        
        site_move[a_sxzy]+=num_move
        position = position.move(site_move)
        surface_love.rect=position
        # 将图像放置在主屏幕上
        pygame.display.flip()
        screen.fill(bg)
        screen.blit(screen_2,(BACKW//20,BACKH//20))
        screen_2.fill(bg_2)

        

# 第一版
        # for xydxyd in xyd_list:
        #     surface_xyd=Snake_ly(surface_xyd_tp,xydxyd)
        #     screen.blit(surface_xyd.image,surface_xyd.rect)

# 第二版
        if len(xyd_list)==0:
            xyd_x=randint(*XYD_SETX)
            xyd_y=randint(*XYD_SETY)
            xyd_list.append((xyd_x,xyd_y))
            surface_xyd=Snake_ly(surface_xyd_tp,xyd_list[0])
        
        screen.blit(surface_xyd.image,surface_xyd.rect)



# 碰撞检测

        # 吃信用点
        crash_result = pygame.sprite.collide_mask(surface_love,surface_xyd)
        if crash_result:
            xyd_list=[]
            num_xydnum+=1
            tishi_txt_4=f"信用点:"+'■'*num_xydnum+'□'*(10-num_xydnum)
            text_4 = f.render(tishi_txt_4,True,(50,20,200),(153,217,234))

            # 变大会变模糊，放弃

        else:
            pass

        # 抓捕
        crash_result_2 = pygame.sprite.collide_mask(surface_love,surface_sw)
        if crash_result_2:
            num_move=0
            die_ly=1

        else:
# 困难模式
            if sw_nandu==2:
                if surface_sw.rect[0]>surface_love.rect[0]:

                    surface_sw.rect[0]-=1
                else:
                    surface_sw.rect[0]+=1

                if surface_sw.rect[1]>surface_love.rect[1]:
                    surface_sw.rect[1]-=1
                else:
                    surface_sw.rect[1]+=1

# 简单模式
            if sw_nandu==1:
                
                if sw_move_rand<=50:
                    if surface_sw.rect[0]>surface_love.rect[0]:
                        surface_sw.rect[0]-=2
                        sw_move_rand+=1
                    else:
                        surface_sw.rect[0]+=2
                        sw_move_rand+=1
                else:
                    if surface_sw.rect[1]>surface_love.rect[1]:
                        surface_sw.rect[1]-=2
                        sw_move_rand+=1
                    else:
                        surface_sw.rect[1]+=2
                        sw_move_rand+=1
                if sw_move_rand>=100:
                    sw_move_rand=0

# 撞墙判定
        if (surface_love.rect[0]<=71)|(surface_love.rect[0]>=1585):
            num_move=-num_move
            # die_ly=1
   
        if (surface_love.rect[1]<=37)|(surface_love.rect[1]>=929):
            num_move=-num_move
            # die_ly=1




# 死亡检测

        if die_ly==1:

            # screen.blit(text_5,textRect_5)
            # pygame.display.update()
            # pygame.time.wait(2000)
            # screen.blit(text_5,textRect_5)
            # screen.blit(surface_lyj, SW_LY)
            # pygame.display.update()
            # pygame.time.wait(1000)
            # screen.blit(text_6,textRect_6)
            # pygame.display.update()
            while True:
                next_ly_sw=0
                for event in pygame.event.get():
                    # x退出
                    if event.type == QUIT:
                        exit()
                    #esc退出 
                    if event.type ==KEYDOWN:
                        if event.key == K_ESCAPE:
                            pygame.quit()
                            sys.exit()
                    # 继续游戏判定
                        if event.key == K_1:
                            next_ly_sw=1
                            die_ly=0
# 初始化

                            num_xydnum=0
                            sudu=1
                            xyd_list=[]
                            site_move = [0, 0]
                            surface_lovey=Snake_ly(surface_lovey_tp,(LY_SETX,LY_SETY))
                            surface_lovez=Snake_ly(surface_lovez_tp,(LY_SETX,LY_SETY))
                            surface_love=Snake_ly(surface_love_tp,(LY_SETX,LY_SETY))
                            surface_sw=Snake_sw(surface_sw_tp,(BACKW-400,BACKH-300))
                            position = surface_love.rect
                            position[0]=LY_SETX
                            position[1]=LY_SETY
                            num_move=1
                        if event.key == K_2:
                            exit()

                if next_ly_sw==1:
                    break
                
        screen.blit(text_4,textRect_4)
        screen.blit(surface_love.image, surface_love.rect)
        screen.blit(surface_sw.image, surface_sw.rect)


        pygame.display.update()